<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/7/15
 * Time: 15:45
 */

namespace app\commi\controller;

use think\Db;
use think\Request;

class Sailingyy extends Base
{
    /**
     * 帆船临时成绩异议列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        $match = Db::table('up_match')
            ->where('match_committee_id', session('com_id'))
            ->field('match_id')
            ->find();
        
        if(request()->isAjax ()){
//            $keyword = trim(input('keyword'), ' '); // 船长姓名/ID
            $ls_status = input('ls_status'); // 状态
            $where['umd.match_id'] = $match['match_id'];
            $where['umd.flag'] = 3;
            
//            if(isset($keyword) && $keyword != ''){
//                $where['user_real_name|user_create_id'] = ['like', '%' . $keyword . '%'];
//            }
            if(isset($ls_status) && $ls_status != ''){
                $where['umar.ls_status'] = $ls_status;
            }else{
                $where['umar.ls_status'] = ['NEQ', 1];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up__match_arrive_record')
                ->alias('umar')
                ->join('up__match_detail umd', 'umar.match_detail_id = umd.id', 'left')
                ->join('up_match_group umg', 'umg.mg_id = umd.level_id', 'left')
                ->join('up_u_user uuu', 'uuu.user_id = umar.user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up__match_arrive_record')
                ->alias('umar')
                ->join('up__match_detail umd', 'umar.match_detail_id = umd.id', 'left')
                ->join('up_match_group umg', 'umg.mg_id = umd.level_id', 'left')
                ->join('up_u_user uuu', 'uuu.user_id = umar.user_id', 'left')
                ->field('umar.id, umg.mg_name, umd.turn, umar.temp_score, umar.sensor_id, umd.match_id, 
                umar.ls_change_score, umar.ls_yiyi_content, umar.ls_commit_time, umar.ls_status, 
                uuu.user_phone, user_real_name, user_create_id, uuu.user_id')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('umar.ls_commit_time desc')
                ->select();
            foreach($lists as $k => $v){
                // 名次
                $lists[$k]['mc'] = $k + 1;
                // 报名信息
//                if($v['sensor_id']){
//                    $sensor = explode('_', $v['sensor_id']);
//                    $user = Db::table('up_u_user')
//                        ->where('user_id', $sensor[1])
//                        ->field('user_phone, user_real_name, user_create_id')
//                        ->find();
                $apply = Db::table('up_match_apply_info')
                    ->where('ma_user_id', $v['user_id'])
                    ->where('ma_match_id', $v['match_id'])
                    ->field('ma_fan_code')
                    ->find();
                $lists[$k]['fan_code'] = $apply['ma_fan_code'];
//                $lists[$k]['user_phone'] = $user['user_phone'];
//                $lists[$k]['user_real_name'] = $user['user_real_name'];
//                $lists[$k]['user_create_id'] = $user['user_create_id'];
//                }
                $lists[$k]['member_type'] = session('cm_member_type');
            }
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        
        return $this->fetch();
    }
    
    /**
     * 修改临时成绩
     * @return mixed|\think\response\Json
     */
    public function changeMark(){
        $id = input('id');
        if(Request::instance()->isPost()){
            $ls_change_score = input('ls_change_score');
            $ls = Db::table('up__match_arrive_record')
                ->where('id', $id)
                ->field('temp_score')
                ->find();
            if($ls_change_score == $ls['temp_score']){
                return json(['code' => 100, 'msg' => '和原成绩相同']);
            }else{
                $update = [
                    'ls_change_score' => $ls_change_score,
                    'ls_status' => 3,
                ];
                $res = Db::table('up__match_arrive_record')->where('id', $id)->update($update);
                if($res){
                    return json(['code' => 200, 'msg' => '修改成功']);
                }else{
                    return json(['code' => 100, 'msg' => '修改失败']);
                }
            }
        }
        $this->assign([
            'id' => $id
        ]);
        $lose = $this->lostTime();
        if($lose == 1){
            return $this->fetch('/lose');
        }else{
            return $this->fetch('sailingyy/changemark');
        }
    }
    
    /**
     * 确认修改成绩
     * @return \think\response\Json
     */
    public function commitMark(){
        $lose = $this->lostTime();
        if($lose == 1){
            return json(['code' => 100, 'msg' => '账号已过期，非法操作']);
        }
        $id = input('id');
        $ls = Db::table('up__match_arrive_record')
            ->where('id', $id)
            ->field('ls_change_score, temp_score')
            ->find();
        Db::startTrans();
        try{
            /* 修改的成绩比原成绩值大，说明名次下降，对应之前名次靠后的，值减一，
        反之，修改的成绩比原来的值小，说明名称上升，对应之前名次靠前的，值加一*/
            if($ls['ls_change_score'] > $ls['temp_score']){
                $where['temp_score'] = [['>', $ls['temp_score']], ['<=', $ls['ls_change_score']], 'and'];
                Db::table('up__match_arrive_record')->where($where)->setDec('temp_score');
            }else{
                $where['temp_score'] = [['>=', $ls['ls_change_score']], ['<', $ls['temp_score']], 'and'];
                Db::table('up__match_arrive_record')->where($where)->setInc('temp_score');
            }
            Db::table('up__match_arrive_record')->where('id', $id)->update(['temp_score' => $ls['ls_change_score'], 'ls_status' => 5]);
            Db::commit();
            return json(['code' => 200, 'msg' => '操作成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 驳回成绩
     * @return \think\response\Json
     */
    public function refsualMark(){
        $lose = $this->lostTime();
        if($lose == 1){
            return json(['code' => 100, 'msg' => '账号已过期，非法操作']);
        }
        $id = input('id');
        $update = [
            'ls_change_score' => null,
            'ls_yiyi_content' => null,
            'ls_status' => 4
        ];
        $res = Db::table('up__match_arrive_record')->where('id', $id)->update($update);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}